﻿using System;
using System.Collections.Generic;
using SlimDX.D3DCompiler;
using SlimDX.Direct3D10;
using D3D9 = SlimDX.Direct3D9;

namespace HlslCompiler
{
    internal class Compiler10
        : ICompiler
    {

        public List<Feedback> Compile(string code, CompilerSettings settings)
        {
            var feedback = GetFeedback(code, settings);
            return feedback != null ? FeedbackParser.Parse(feedback) : null;
        }

        private string GetFeedback(string code, CompilerSettings settings)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(code)) throw new Exception("There is no code to compile.");
                Effect.FromString(Device, code, settings.Profile, settings.ShaderFlags, settings.EffectFlags);
            }
            catch (Exception exception)
            {
                if (string.IsNullOrWhiteSpace(exception.Message)) return "Unspecified compilation error.";
                return exception.Message;
            }
            return null;
        }

        private Device _device;
        private Device Device { get { return _device ?? (_device = new Device(DeviceCreationFlags.None)); } }
    }
}